<?php
/**
 * Created by PhpStorm.
 * User: tsy
 * Date: 2016/7/14
 * Time: 11:14
 */

namespace Admin\Controller\Admin;
use Think\Controller;
use Admin\Controller\Common\BaseController;

class RoleController extends BaseController{

    public function role_main(){
        $role=D("Role");
        $role_array=$role->select_role();
        $json=json_encode($role_array);

        $this->assign("role_json",$json);
        $this->display();
    }

    public function role_add(){
        $authority=D("Authority");
        $authority_array=$authority->get_authority_all();

        $authority_json=json_encode($authority_array);

        $this->assign("authority_json",$authority_json);
        $this->display();
    }

    public function role_add_ajax(){
        $role_name=I("post.role_name");
        $role_code=I("post.role_code");
        $authority_id=I("post.authority_id");

        $role_array["role_name"]=$role_name;
        $role_array["role_code"]=$role_code;

        $role=D("Role");
        $role_result=$role->data($role_array)->add();
        if($role_result){
            $role_id = $role_result;
            $authority_count=count($authority_id);

            $role_authority=D("bypal_comm_roleauthority");
            $role_authority_array=array();
            for($i=0;$i<$authority_count;$i++){
                $role_authority_array[]=array("role_id"=>$role_id,"authority_id"=>$authority_id[$i]);
            }
            $role_authority->addAll($role_authority_array);
            $result['state']=1;
        }else{
            $result['state']=2;
        }

        $this->ajaxReturn(json_encode($result));
    }

    public function validate_role_code(){
        $name=I("get.fieldId");
        $value=I("get.fieldValue");
        $role_id=I("get.role_id","");
        $role=D("Role");
        $role_array=$role->select_find_by_code($value,$role_id);
        $count=count($role_array);
        $result[0]=$name;
        if($count!=0){
            $result[1]=false;
        }else{
            $result[1]=true;
        }
        echo json_encode($result);
    }

    public function role_edit(){
        $role_id=I("get.role_id");

        $authority=D("Authority");
        $authority_array=$authority->get_authority_by_role($role_id);

        $role=D("Role");
        $role_array=$role->select_find_by_id($role_id);
        $this->assign("authority_json",json_encode($authority_array));
        $this->assign("role_array",$role_array[0]);
        $this->display();
    }

    public function role_edit_ajax(){
        $role_name=I("post.role_name");
        $role_code=I("post.role_code");
        $role_id=I("post.role_id");
        $authority_id=I("post.authority_id");

        $role_array["role_name"]=$role_name;
        $role_array["role_code"]=$role_code;

        $role=D("Role");
        $role_result=$role->where("role_id=$role_id")->save($role_array);
        \Think\Log::write($role_result,'WARN');
        if($role_result>=0){
            $authority_count=count($authority_id);

            $role_authority=D("bypal_comm_roleauthority");

            $role_authority->where("role_id=$role_id")->delete();
            $role_authority_array=array();
            for($i=0;$i<$authority_count;$i++){
                $role_authority_array[]=array("role_id"=>$role_id,"authority_id"=>$authority_id[$i]);
            }
            $role_authority->addAll($role_authority_array);
            $result['state']=1;
        }else{
            $result['state']=2;
        }

        $this->ajaxReturn(json_encode($result));
    }
    public function button_authority(){
        $pathname=I("post.pathname");
        $role_id=$_SESSION['role_id'];
        $authority=D("authority");
        $authority_array=$authority->get_button_authority($pathname,$role_id);

        $authority_count=count($authority_array);

        $html_str='<script type="text/javascript" src="../../../Public/easyui/jquery.easyui.min.js"></script>';
        for($i=0;$i<$authority_count;$i++){
            $button_function=$authority_array[$i]['button_function'];
            $button_type=$authority_array[$i]['button_type'];
            if($button_type=="add"){
                $html_str.='<a href="javscript:;" onclick="'.$button_function.';" class="easyui-linkbutton" iconCls="icon-add" plain="true">新增</a>';
            }else if($button_type=="edit"){
                $html_str.='<a href="javscript:;" onclick="'.$button_function.';" class="easyui-linkbutton" iconCls="icon-edit" plain="true">编辑</a>';
            }else if($button_type=='man'){
                $html_str.='<a href="javscript:;" onclick="'.$button_function.';" class="easyui-linkbutton" iconCls="icon-man" plain="true">详情</a>';
            }else if($button_type=='cancel'){
                $html_str.='<a href="javscript:;" onclick="'.$button_function.';" class="easyui-linkbutton" iconCls="icon-cancel" plain="true">取消</a>';
            }else if($button_type=='money'){
                $html_str.='<a href="javscript:;" onclick="'.$button_function.';" id="paymoney" style="display:none" class="easyui-linkbutton" iconCls="icon-money" plain="true">放款</a>';
            }else if($button_type=='upload'){
                $html_str.='<a href="javscript:;" onclick="'.$button_function.';" class="easyui-linkbutton" iconCls="icon-filter" plain="true">文件上传</a>';
            }else if($button_type=='large-smartart'){
                $html_str.='<a href="javscript:;" onclick="'.$button_function.';" class="easyui-linkbutton" iconCls="icon-large-smartart" plain="true">审核</a>';
            }
        }
        $this->ajaxReturn($html_str);
    }
}